package org.szgb.console.sys.custom.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.custom.entity.SysCustomFormItemModel;
import org.szgb.console.sys.custom.mapper.SysCustomFormItemModelMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysCustomFormItemModelService {
    private final SysCustomFormItemModelMapper sysCustomFormItemModelMapper;

    @Autowired
    public SysCustomFormItemModelService(SysCustomFormItemModelMapper sysCustomFormItemModelMapper){
        this.sysCustomFormItemModelMapper = sysCustomFormItemModelMapper;
    }

    @Transactional(readOnly = true)
    public SysCustomFormItemModel get(String id) {
        return sysCustomFormItemModelMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysCustomFormItemModel> findList(SysCustomFormItemModel sysCustomFormItemModel) {
        if (sysCustomFormItemModel != null && sysCustomFormItemModel.getPageDto() != null) {
            Page<SysCustomFormItemModel> page = new Page<>(sysCustomFormItemModel.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysCustomFormItemModel.setPage(page);
        }
        return sysCustomFormItemModelMapper.findList(sysCustomFormItemModel);
    }

    @Transactional(readOnly = true)
    public Integer getCount(SysCustomFormItemModel sysCustomFormItemModel) {
        return sysCustomFormItemModelMapper.getCount(sysCustomFormItemModel);
    }

    @Transactional()
    public void save(SysCustomFormItemModel sysCustomFormItemModel) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysCustomFormItemModel.getId() == null || sysCustomFormItemModel.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysCustomFormItemModel.setCreatedBy(sysCustomFormItemModel.getUpdatedBy());
            // 添加UUID
            sysCustomFormItemModel.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysCustomFormItemModelMapper.insert(sysCustomFormItemModel);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysCustomFormItemModelMapper.update(sysCustomFormItemModel);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysCustomFormItemModelMapper.delete(ids.split(","));
    }
}
